package com.studentstreet.tools;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.widget.EditText;

/**
 * @author likebamboo
 * 
 */
public class PreferenceTools {
	private Context context = null;
	private String prefName = "";

	public PreferenceTools(Context context) {
		this.context = context;
		prefName = "userData";
	}

	public PreferenceTools(Context context, String prefName) {
		this.context = context;
		this.prefName = prefName;
	}

	// 获得用户名
	public String getUserName() {
		SharedPreferences pref = context.getSharedPreferences(this.prefName,
				Context.MODE_PRIVATE);
		return pref.getString("userName", "");
	}

	public String getUserPwd() {
		SharedPreferences pref = context.getSharedPreferences(this.prefName,
				Context.MODE_PRIVATE);
		return pref.getString("password", "");
	}

	// 保存用户登录信息
	public boolean saveLoginInfo(String userName, String password) {
		SharedPreferences pref = context.getSharedPreferences("userData",
				Context.MODE_PRIVATE);
		Editor editor = pref.edit();
		editor.putString("userName", userName);
		editor.putString("password", password);
		editor.putBoolean("isLogin", true);
		return editor.commit();
	}

	public boolean saveLoginInfo(String userName) {
		SharedPreferences pref = context.getSharedPreferences("userData",
				Context.MODE_PRIVATE);
		Editor editor = pref.edit();
		editor.putString("userName", userName);
		editor.putString("password", "");
		editor.putBoolean("isLogin", true);
		return editor.commit();
	}

	// 保存是否登录信息
	public boolean saveIsLogin() {
		SharedPreferences pref = context.getSharedPreferences("userData",
				Context.MODE_PRIVATE);
		Editor editor = pref.edit();
		// 密码没有，说明没有登录
		if ("".equals(getUserPwd())) {
			editor.putBoolean("isLogin", false);
		} else {
			editor.putBoolean("isLogin", true);
		}

		return editor.commit();
	}

	// 获得是否登录信息
	public boolean isLogin() {
		SharedPreferences pref = context.getSharedPreferences("userData",
				Context.MODE_PRIVATE);
		return pref.getBoolean("isLogin", false);
	}

	// OEM++ liwt@2013-02-21 注销用户
	public boolean cleanUserInfo() {
		SharedPreferences pref = context.getSharedPreferences("userData",
				Context.MODE_PRIVATE);
		Editor editor = pref.edit();
		editor.clear();
		return editor.commit();

	}

	/**
	 * 保存用户地址经度信息
	 * 
	 * @param lon
	 *            经度
	 * @param lat
	 *            纬度
	 * @return 保存结果
	 */
	public boolean saveLoc(float lon, float lat, String loc) {
		SharedPreferences pref = context.getSharedPreferences("userData",
				Context.MODE_PRIVATE);
		Editor editor = pref.edit();
		editor.putFloat("lon", lon);
		editor.putFloat("lat", lat);
		editor.putString("loc", loc);
		return editor.commit();
	}

	public float getLon() {
		SharedPreferences pref = context.getSharedPreferences("userData",
				Context.MODE_PRIVATE);
		return pref.getFloat("lon", 0.0F);
	}

	public float getLat() {
		SharedPreferences pref = context.getSharedPreferences("userData",
				Context.MODE_PRIVATE);
		return pref.getFloat("lat", 0.0F);
	}

	public String getLoc() {
		SharedPreferences pref = context.getSharedPreferences("userData",
				Context.MODE_PRIVATE);
		return pref.getString("loc", "");
	}
}
